<?php

namespace app\admin\controller;

use think\Lang;

/**
 *服务人员等级说明
 */
class Fugrade extends AdminControl {

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/fugrade.lang.php');
    }

    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        $model = model('fugrade');
        $condition =[];
        /**
         * 检索条件
         */
        if (!empty(trim(input('search_name'))))
        {
            $condition['name'] = array('like','%'.trim(input('search_name')).'%');
        }
        if (!empty(trim(input('search_admin_name'))))
        {
            $condition['admin_name'] = array('like','%'.trim(input('search_admin_name')).'%');
        }
        if (!empty(trim(input('search_status'))))
        {
            $status = input('search_status');
            if ($status == 2){
                $status=0;
            }
            $condition['status'] = $status;
        }
        $data =$model->getList($condition,'*',10);
        $this->assign('data',$data);
        $this->assign('showpage',$model->page_info->render());
        $this->assign('search_name',trim(input('search_name')));
        $this->assign('search_admin_name',trim(input('search_admin_name')));
        $this->assign('search_status',trim(input('search_status')));
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $model = model('fugrade');
        if (request()->isPost()){
            $validate = validate('fugrade');
            if (!$validate->scene('add')->check($_POST))
            {
                $this->error($validate->getError());
            }
            $_POST['desc'] = htmlentities($_POST['desc']);
            if ($res = $model->add($_POST))
            {
                $this->log(lang('ds_add') . lang('fuwu_grade_D') . '[' . input('post.name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            }else{
                $this->error(lang('ds_common_save_fail'));
            }
        }else {
            $data = [
                'id'=>'',
                'name'=>'',
                'desc'=>'',
                'admin_id'=>session('admin_id'),
                'admin_name'=>session('admin_name'),
                'status'=>1,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            $this->setAdminCurItem('add');
            $this->assign('data',$data);
            return $this->fetch('form');
        }
    }

    /**
     * 修改
     */
    public function edit()
    {
        $model = model('fugrade');
        if (request()->isPost()){
            $validate = validate('fugrade');
            $_POST['updated_at']=date('Y-m-d H:i:s',time());
            if (!$validate->scene('add')->check($_POST))
            {
                $this->error($validate->getError());
            }
            $_POST['desc'] = htmlentities($_POST['desc']);
            if ($res = $model->edit(['id'=>$_POST['id']],$_POST))
            {
                $this->log(lang('ds_update') . lang('fuwu_grade_D') . '[' . input('post.name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            }else{
                $this->log(lang('ds_update') . lang('fuwu_grade_D') . '[' . input('post.name') . ']', 0);
                $this->error(lang('ds_common_save_fail'));
            }
        }else {
            $data = $model->info(['id'=>input('id')]);
            $this->setAdminCurItem('edit');
            $this->assign('data',$data);
            return $this->fetch('form');
        }
    }

    /**
     * 修改状态
     */
    public function edit_status()
    {
        $model = model('fugrade');
        $condition['id'] = input('id');
        $data[input('column')] = input('value');
        $info = $model->edit($condition,$data);
        if ($info) {
            $this->log(lang('ds_edit') . lang('fuwu_grade_D') . '[ID:' . intval(input('id')) . ')', 1);
            ds_json_encode(10000, lang('ds_common_op_succ'));
        }else{
            $this->log(lang('ds_edit') . lang('fuwu_grade_D') . '[ID:' . intval(input('id')) . ')', 0);
            ds_json_encode(10001, lang('ds_common_op_fail'));
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $ids = trim(input('id'));
        $idArr = ds_delete_param($ids);
        if ($idArr == FALSE)
        {
            $this->log(lang('ds_del') . lang('fuwu_grade_D') . '[ID:' . $ids . ']', 0);
            ds_json_encode(10001, lang('param_error'));
        }
        $model = model('fugrade');
        $model->del(array('id'=>array('in',implode(',',$idArr))));
        $this->log(lang('ds_del') . lang('fuwu_grade_D') . '[ID:' . $ids . ']', 1);
        ds_json_encode(10000, lang('ds_common_del_succ'));
    }
    /**
     * ajax
     */
    public function ajax()
    {
        $model = model('fugrade');
        switch (input('branch'))
        {
            case 'check_name':
                $condition['name']=trim(input('name'));
                $condition['id']=['neq',input('id')];
                $data = $model->getList($condition,'id');
                if (empty($data))
                {
                    exit('true');
                }else{
                    exit('false');
                }
        }

    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Fugrade/index'),
            ),
            array(
                'name' => 'add',
                'text' => lang('ds_add'),
                'url' => url('Fugrade/add'),
//                    "javascript:dsLayerOpen('".url('Fugrade/add')."','".lang('ds_add')."')"
            ),
        );
        if (request()->action() == 'edit') {
            $menu_array[] = array(
                'name' => 'edit',
                'text' => lang('ds_edit'),
                'url' => 'javascript:void(0)'
            );
        }
        return $menu_array;
    }
}

?>
